/*********************************************************************************
  *Copyright (C), 2016-2023, jiance.zhang
  *FileName:  xcp_task.c
  *Author:  JianCe.Zhang
  *Version:  V1.0
  *Date: 2024-05-02
  *Description: 
  *Function List:
  * 1. void function(void)
  * 2. 
  * 3. 
  * 4. 
  *History:
  * 1. 2024-05-02;JianCe.Zhang;Init Function 
  * 2. 
  * 3. 
  * 4. 
****************************************Includes***********************************/
#include "main.h"
#include "FreeRTOS.h"
#include "task.h"
#include "CAN_Module.h"
#include "XcpBasic.h"
#include "xcp_task.h"


/***************************************Variables***********************************/
XCP_Event_t XCP_Event_Config[]=
{
    {
        .event_chanel = 0,
        .envet_cycle = 10,
    },
    {
        .event_chanel = 1,
        .envet_cycle = 100,
    }
};


/***************************************Functions***********************************/

void XCP_Task(void *pvParameters)
{
  uint8_t XcpRxData[8];
  while (1)
  {
    /* check for received data */
    if (CAN_Module_Read(0, 0x200, 0, 0, XcpRxData) > 0)
    {
      XcpCommand((vuint32 *)XcpRxData);
    }
    /* XCP background handling */
    XcpBackground();
    while (CAN_Module_CheckIdle(0))
    {
      if (0 == XcpSendCallBack())
      {
        vTaskDelay(10);
        break;
      }
    }
  }
}

uint32_t event_10ms_count = 0;
uint32_t event_100ms_count = 0;

void XCP_Event_Task(void *pvParameters)
{
  XCP_Event_t* event = (XCP_Event_t*)pvParameters;
  TickType_t xLastWakeTime=xTaskGetTickCount();
  while (1)
  {
    vTaskDelayUntil(&xLastWakeTime, event->envet_cycle);
    XcpEvent(event->event_chanel);
    if(event->envet_cycle == 10)
    {
      event_10ms_count++;
    }
    else if(event->envet_cycle == 100)
    {
      event_100ms_count++;
    }
  }
}